import com.jack.common.exception.Assert;

import java.util.Arrays;

public class test {

    public int[] searchRange(int[] nums, int target) {
        if(nums == null || nums.length == 0){
            return new int[]{-1, -1};
        }
        int index = binarySearch(nums, target);
        if(index == -1){
            return new int[]{-1, -1};
        }else{
            int left = index, right = index;
            while(left >=0 && nums[left] == target){
                left--;
            }
            while(nums[right] == target){
                right++;
            }
            return new int[]{left + 1, right - 1};
        }
    }

    private int binarySearch(int[] nums, int target){
        int left = 0, right = nums.length - 1;
        while(left < right){
            int mid = (left + right) / 2;
            if(nums[mid] == target){
                return mid;
            }else if(nums[mid] > target){
                right = mid - 1;
            }else{
                left = mid + 1;
            }
        }
        return -1;
    }

    public class Num{
        int i = 1;
        boolean flag = false;
    }
    public class PrintQi implements Runnable{
        Num num ;
        public PrintQi(Num num){
            this.num = num;
        }
        public void run(){
            while(num.i<= 100){
                synchronized (num) {
                    if(num.flag){
                        try {
                            num.wait();
                        } catch (Exception e) {}
                    }
                    else {
                        System.out.println("奇数"+num.i);
                        num.i++;
                        num.flag = true;
                        num.notify();
                    }
                }
            }
        }
    }
    public class PrintOu implements Runnable{
        Num num ;
        public PrintOu(Num num){
            this.num = num;
        }
        public void run(){
            while(num.i<= 100){
                synchronized (num) {
                    if(!num.flag){
                        try {
                            num.wait();
                        } catch (Exception e) {}
                    }
                    else {
                        System.out.println("偶数"+num.i);
                        num.i++;
                        num.flag = false;
                        num.notify();
                    }
                }
            }
        }
    }



    public static void main(String[] args) {
//        Num num = new Num();
//        PrintQi pQi = new PrintQi(num);
//        PrintOu pOu = new PrintOu(num);
//        Thread aThread = new Thread(pQi);
//        Thread bThread = new Thread(pOu);
//        aThread.start();
//        bThread.start();

    }


    private static int[] mergeSort(int[] nums){
        Assert.notNull(nums, "the parameter is null");
        int n = nums.length;
        int segment = 2;
        while(segment < n){
            for(int i = 0;i < n;i++){
                int distance = segment/2;
                for(int j = i;j < j+distance && j+distance < n;j++){
                    if(nums[j] > nums[j+distance]){
                        int tmp = nums[j+distance];
                        nums[j+distance] = nums[j];
                        nums[j] = tmp;
                    }
                }
            }
            segment *=2;
        }
        return nums;
    }

}
